//
//  EmotionKeyboardView.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/27.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit

let emotionToolBarH: CGFloat = 37


class EmotionKeyboardView: UIView {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        emotionToolBar.childButtonClickClosure = { (type) in
            
            print(type)//设置要执行的闭包
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            self.emotionCollectView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
 
            self.showPageControl(indexPath)
        }
        
        //添加控件
        addSubview(pageControl)
        addSubview(emotionToolBar)
        addSubview(emotionCollectView)
        
        //添加约束
        emotionToolBar.snp_makeConstraints { (make) -> Void in
            
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(emotionToolBarH)
        }
        
        emotionCollectView.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emotionToolBar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(emotionToolBar.snp_top)
            make.centerX.equalTo(self)
        }
        
        //建立连接
//        emotionToolBar.selectedSection = emotionCollectView.indexSection
        emotionCollectView.scrollViewDidScrollClosure = { [weak self](indexPath) in
            
            self?.emotionToolBar.selectedSection = indexPath.section
            
            self?.showPageControl(indexPath)
        }
        
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            
            let indexPath = NSIndexPath(forItem: 0, inSection: 1)
            self.emotionCollectView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            self.emotionToolBar.selectedSection = indexPath.section
            self.showPageControl(indexPath)
        }
        
    }
    
    //设置pageControl显示的内容
    func showPageControl(indexPath: NSIndexPath){
        
        pageControl.numberOfPages = EmotionKeyboardViewModel.sharViewModel.allEmotions[indexPath.section].count
        pageControl.currentPage = indexPath.item
        pageControl.hidden = indexPath.section == 0
    }
    
    //懒加载控件
    private lazy var pageControl: UIPageControl = {
        
        let pageControl = UIPageControl()
        
        pageControl.currentPageIndicatorTintColor = UIColor.redColor()
        pageControl.pageIndicatorTintColor = UIColor.darkGrayColor()
        
        pageControl.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "_pageImage")
        pageControl.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "_currentPageImage")
//        pageControl.numberOfPages = 8
        pageControl.hidden = true
        
        return pageControl
    }()
    
    private lazy var emotionToolBar = EmotionToolBar(frame: CGRectZero)
    
    private lazy var emotionCollectView = EmotionCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
}
